package com.prs.crm.action.person;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.person.Cultivate;
import com.prs.crm.domain.person.DossierCultivate;
import com.prs.crm.service.person.CultivateService;
import com.prs.crm.service.person.DossierCultivateService;
import com.prs.crm.service.person.impl.DossierCultivateServiceImpl;

public class CultivateAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Cultivate cultivate;
	private CultivateService cultivateService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private DossierCultivate dossierCultivate;
	private DossierCultivateService dossierCultivateService;
	public DossierCultivate getDossierCultivate() {
		return dossierCultivate;
	}
	public void setDossierCultivate(DossierCultivate dossierCultivate) {
		this.dossierCultivate = dossierCultivate;
	}

	public DossierCultivateService getDossierCultivateService() {
		return dossierCultivateService;
	}
	public void setDossierCultivateService(
			DossierCultivateService dossierCultivateService) {
		this.dossierCultivateService = dossierCultivateService;
	}
	public Cultivate getCultivate() {
		return cultivate;
	}
	public void setCultivate(Cultivate cultivate) {
		this.cultivate = cultivate;
	}

	public CultivateService getCultivateService() {
		return cultivateService;
	}
	public void setCultivateService(CultivateService cultivateService) {
		this.cultivateService = cultivateService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	@Action("listCultivate")
	public String listCultivate(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Cultivate.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCultivateService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editCultivate")
	public String editCultivate(){
		if(ids!=null){
			this.cultivate=this.getCultivateService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action("viewCultivate")
	public String viewCultivate(){
		if(ids!=null){
			this.cultivate=this.getCultivateService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveCultivate",results={
			@Result(name="success",type="redirect",location="listCultivate"),
			@Result(name="error",type="chain",location="editCultivate")
	})
	public String saveCultivate(){
		if(cultivate!=null){
		this.getCultivateService().save(cultivate);
		}
		String str=cultivate.getPeopleId();
		String str2[]=str.split(",");
		System.out.println(str);
		if(str!=null){
			for(String x:str2){
				DossierCultivate dos=new DossierCultivate();
				dos.setCultivateId(dossierCultivateService.getDoscurById());
				dos.setDossierId(x);
			if (dos!=null) {
			  this.getDossierCultivateService().save(dos);
		  	} 
		}
	}
		return SUCCESS;
} 
	
	@Action(value="removeCultivate",results={
			@Result(name="success",type="redirect",location="listCultivate"),
			@Result(name="error",type="chain",location="listCultivate")
	})
	public String removeCultivate(){
		if(ids==null){
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getCultivateService().batchRemove(ids);
		return SUCCESS;
	}
}
